<?php
namespace App\Logic\SsBasic;

use App\Models\Basics\Transport;
use App\Models\Product\ProductSku;
use App\Models\SsBasic\AreaSku;
use App\Models\Stocking\StockingSku;
class SyncSSInit
{
    /**
     * 补数据
     */
    public static function exe()
    {
        $skuArr = [];
        $price = [];
        $pCycle = [];
        $transportArr = [];
        $n = 0;
        AreaSku::query()
            ->orderBy('id')
            ->chunk(800, function ($ss) use (&$skuArr,&$price,&$pCycle,&$transportArr,&$n){
                foreach($ss as $_ss){
                    $n++;
                    $count = StockingSku::query()
                        ->where('area_psku',$_ss->area_psku)
                        ->count();
                    echo '=========='.$n.'====已存在：'.$_ss->area_psku.'========='.PHP_EOL;
                    if($count >0)continue;
                    echo '=========='.$n.'====start：'.$_ss->area_psku.'========='.PHP_EOL;
                    list($areaId,$pSkuId) = explode('_',$_ss->area_psku);
                    if(!array_key_exists($pSkuId,$skuArr)){
                        $skuArr[$pSkuId] = ProductSku::getModelById($pSkuId);
                    }
                    $sku = $skuArr[$pSkuId];
                    if(!array_key_exists($pSkuId,$price)){
                        $price[$pSkuId] = ProductSku::getPuPrice($sku);
                    }
                    if(!array_key_exists($pSkuId,$pCycle)){
                        $pCycle[$pSkuId] = ProductSku::getPuCycle($sku);
                    }
                    if(!array_key_exists($areaId,$transportArr)){
                        $transportArr[$areaId] = Transport::getTransportDay($areaId);
                    }
                    $transport = $transportArr[$areaId];
                    AreaSku::addAreaSkuData($areaId,$pSkuId,$transport->air_day ?? 0,$transport->sea_day ?? 0,$pCycle[$pSkuId],$price[$pSkuId]);
                    echo '=========='.$n.'====end：'.$_ss->area_psku.'========='.PHP_EOL;
                }
            });
    }
}
